package com.qimo.zuoyeoa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qimo.zuoyeoa.dao.UserMapper;
import com.qimo.zuoyeoa.entity.User;
import com.qimo.zuoyeoa.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByNamePass(String name, String password) {
        return userMapper.selectByNamePass(name,password);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean append(User user) {
        return userMapper.insert(user) > 0;
    }

    @Override
    public PageInfo<User> findAll(int pageNum, int pageSize) {

        PageHelper.startPage(pageNum,pageSize);
        final List<User> users = userMapper.selectAll();
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public PageInfo<User> findByKeyword(int pageNum, int pageSize, String keyword) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userMapper.selectByKeyword(keyword);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public User findById(int id) {
        return userMapper.selectById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean modify(User user) {
        return userMapper.updateById(user)>0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean remove(int id) {
        return userMapper.deleteById(id) > 0;
    }

    @Override
    public List<User> findAll() {
        return userMapper.selectAll();
    }
}
